﻿using System.Linq;
using System.Text;

namespace Kindler.IO.MobiFile
{
    public static class Extensions
    {
        public static MobiFileHeader ToHeader(this MobiFileRecord record)
        {
            return MobiFileReader.Create<MobiFileHeader>(record.Value);
        }

        public static MobiFileHeader GetHeader(this MobiFile file)
        {
            return file.Records.First().ToHeader();
        }

        public static string GetFullName(this MobiFile file)
        {
            MobiFileHeader header = file.GetHeader();
            byte[] record0 = file.Records.First().Value;

            return Encoding.UTF8.GetString(record0.Skip((int)header.FullNameOffset).Take((int)header.FullNameLength).ToArray());
        }

        public static string GetHtml(this MobiFile file)
        {
            MobiFileHeader header = file.GetHeader();
            string html = string.Join("", file.Records.Skip(1).Take((int)header.FirstNonBookIndex - 1).Select(r => Encoding.UTF8.GetString(r.Value)));

            return html;
        }
    }
}